package com.self.singliton;

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 作者：tianjiayuan
 * 创建时间：2018-03-23 18:50
 * 类描述：
 * 修改人：
 * 修改时间：
 */
public class SinglItonTest {

    private static SinglItonTest instance = null;

    private SinglItonTest() {
        System.out.println("create singiton");
    }

    private static SinglItonTest getInstance() {
        if (instance == null) {
            instance = new SinglItonTest();
        }
        return instance;
    }

    public static void main(String[] args) {
        SinglItonTest ss = SinglItonTest.getInstance();
        SinglItonTest s1 = SinglItonTest.getInstance();

        Tt t = new Tt();

        FutureTask<Integer> future = new FutureTask<Integer>(t);

        new Thread(future).start();


        try {
            System.out.println(future.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }

    static class Tt implements Callable<Integer> {
        /**
         * Computes a result, or throws an exception if unable to do so.
         *
         * @return computed result
         * @throws Exception if unable to compute a result
         */
        @Override
        public Integer call() throws Exception {
            int t = new Random().nextInt(100);
            System.out.println(t);
            return t;
        }
    }
}
